package org.ycy.data.generator.sink;

import cn.hutool.core.io.FileUtil;
import org.ycy.data.generator.constants.Symbol;
import org.ycy.data.generator.entity.TemplateConfig;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: data-generator
 * @ClassName FileSink
 * @description:
 * @author: ycy
 * @create: 2023-06-16 22:46
 * @Version 1.0
 **/
public class FileSink extends Sink {

    public FileSink(TemplateConfig templateConfig) {
        super(templateConfig);
    }

    @Override
    public String createHeader() {
        headers = templateConfig.getColumns().stream().map(item -> item.getColumnCode()).collect(Collectors.joining(Symbol.COMMA));
        FileUtil.appendLines(Collections.singletonList(headers), file, Charset.defaultCharset());
        return headers;
    }


    @Override
    protected void append(List<List<Object>> data) {

        List<String> lines = data.stream().map(item ->
                item.stream().map(cl -> String.valueOf(cl)).collect(Collectors.joining(Symbol.COMMA))).collect(Collectors.toList());
        FileUtil.appendLines(lines, file, Charset.defaultCharset());
        data.clear();
    }

    @Override
    public void close() {

    }
}